﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WeChat.UI.Command
{
    public class Command<T> : CommandBase
    {
        readonly Func<T, bool> _canExecute;
        readonly Action<T> _execute;

        public Command(Action<T> execute, Func<T, bool> canExecute = null)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public override bool CanExecute(object parameter)
        {
            if (_canExecute is null)
                return true;
            if (parameter is T t)
                return _canExecute(t);
            return false;
        }

        public override void Execute(object parameter)
        {
            _execute?.Invoke(parameter is T t ? t : default);
        }

    }
}
